let User = require('../model/user')
let User_info = require('../model/user_info')
let result = require('../utils/result')
let format = require('../utils/format.js')
function UserInfoController(){

};

/**
 * 查询所有用户
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
UserInfoController.prototype.getUserInfo = async (req,res)=>{
    let uid = req.body.uid;
    if(!uid){
        res.json(result(-1,'请求参数有误'))
        return;
    };
    let UserInfo = await User_info.findOne({where:{uid:uid}});
    if(UserInfo){
    res.status(200).json(result(0,'success',UserInfo))
    }else{
        res.status(200).json(-1,'无信息',[])
    }
}


UserInfoController.prototype.createUserInfo = async (req,res)=>{
    let uid = req.body.uid;
    if(!uid || !req.body.uname || !req.body.sex || !req.body.age || !req.body.contact){
        res.json(result(-1,'请求参数有误'))
        return;
    };
    let user_info = await User_info.findAll({where: {uid:uid}});
    if(user_info&&user_info.length){

        User_info.update({
            uname:req.body.uname,
            sex:req.body.sex,
            age:req.body.age,
            contact:req.body.contact,
            qq:req.body.qq,
            wechat:req.body.wechat
        },{where:{uid:uid}}).then(()=>{
            res.json(result(0,'success'))
        })
    }else{
        User_info.create(req.body).then(rsp=>{
            res.json(result(0,'success'))
        }).catch(()=>{
            res.json(result(-1,'fail'))
        })
    }
}


UserInfoController.prototype.user = async (req,res)=>{
    let uid = req.body.uid;
    let user = await User.findOne({where:{uid:uid}});
    if(user){
        res.json(result(0,'success',user))
    }else{
        res.json(result(-1,'fail'))
    }
}
let userInfoController = new UserInfoController();
module.exports = userInfoController
